


clear all
set more off
pause off
set matsize 11000

capture confirm file "Appendix_Results/"
if _rc mkdir "Appendix_Results/"


capture confirm file "Appendix_Results/gphs/"
if _rc mkdir "Appendix_Results/gphs/"





import excel "Data/SPPUKQ.xls", sheet("FRED Graph") cellrange(A11:B318) firstrow clear
ren observation_date year
sort year
save Data/UKStocks.dta, replace


use "Data/Big_Pandemics.dta", clear
gen pandemic=1



drop year
ren endyear year
sort year
merge 1:1 year using "Data/RealRateSince1310.dta"
sort year
replace pandemic=0 if pandemic==.
gen pandemic100k=cond(dead>=100000,pandemic,0)
cap drop Horizon
gen Horizon = _n-1 if _n<=41
merge 1:1 year using "Data/sdeaths.dta" , nogen
replace sdeaths =0 if sdeaths==.
replace sdeaths= cond(sdeaths>20000,1,0)
sort year

merge 1:1 year using "Data/UKStocks.dta" , nogen
sort year
rename SPPUKQ stocksUK
replace stocksUK = 100*log(stocksUK)

cap drop Horizon
gen Horizon = _n-1 if _n<=41
tsset year

local response_raw EuroR_real stocksUK	
foreach y of local response_raw {

	gen b0_`y'=0 if Horizon==0
	gen se0_`y'=0 if Horizon==0	
	gen h0_`y'=0 if Horizon==0
	gen l0_`y'=0 if Horizon==0
	gen h1_`y'=0 if Horizon==0
	gen l1_`y'=0 if Horizon==0
	
	}

tsset year
local response_trend EuroR_real  
	foreach y of local response_trend {
		

	
cap drop raw
gen raw = .
replace raw = `y'


/********** State space specification starts here *************/

constraint 1	[rstar]L.rstar		= 1		/* Random Walk */	
constraint 2	[raw]rstar			= 1		/* Definitional */


cap drop zero								/* generate zero line for figures */
gen zero = 0

tsset year
cap drop r_star

/************	 State space equations	 ******************/

sspace		(rstar L.rstar, state noconstant)		/// /* r-star */
			(raw rstar, noconstant ),				/// /* raw */
			covstate(identity) constraints(1/2) difficult /*method(kdiffuse)*/ iterate(25)
			
			qui{
			/* Generate smoothed estimates of r-star */
			predict r_star if e(sample), states smethod(smooth) equation(rstar)
			
			
		
		
			cap drop `y'trend
			gen	`y'trend = r_star
			}
		}				



// estimation on raw real rate  series								
foreach y of local response_raw {
	eststo clear
		forv h=0/40 {
		
						cap drop  d`y'`h'
						gen d`y'`h' = f`h'.`y'- l.`y'
						reg d`y'`h' pandemic100k l(0/10).pandemic100k  l(2/11).`y' 
												
						eststo
						replace b0_`y'  = _b[pandemic100k]                      if Horizon==`h'
						replace se0_`y' = _se[pandemic100k]                     if Horizon==`h'
						replace h0_`y'  = _b[pandemic100k]+2*_se[pandemic100k]  if Horizon==`h'
						replace l0_`y'  = _b[pandemic100k]-2*_se[pandemic100k]  if Horizon==`h'
						replace h1_`y'  = _b[pandemic100k]+1*_se[pandemic100k]  if Horizon==`h'
						replace l1_`y'  = _b[pandemic100k]-1*_se[pandemic100k]  if Horizon==`h'
						
						
					
						}
						



}

// Figure A5 b
	cap drop Zero
		gen Zero=0
		twoway ///
		(  rarea h0_stocksUK l0_stocksUK Horizon if Horizon~=. , fc(green)  fi(10) lcolor(white)  lpattern(none)  )  ///
		(  rarea h1_stocksUK l1_stocksUK Horizon if Horizon~=. , fc(green)  fi(20) lcolor(white) lw(none)  lpattern(none)  )  ///
		(  line  Zero    Horizon if Horizon~=. , lc(gray)  lw(med) lp(dash)                     )  ///		
		(  line  b0_stocksUK     Horizon if Horizon~=. , lc(green)  lw(thick) lp(solid)  ///
				xlab(0(10)40, labsize(large)) ylab(, angle(0) labsize(large)  nogrid) xtitle("Years since pandemic event ended",size(large)) ///
				ytitle("Percent",size(large)) ///
				legend( off ) )  ///
		, title()    ///
		xsize(5) ysize(4) scale(1.1) graphregion(color(white)) plotregion(color(white))  scheme(s1color) ///
		name(stocksUK,replace) /// nodraw
	
		graph save   Appendix_Results/gphs/FigureA5b.gph, replace
		graph export Appendix_Results/FigureA5b.pdf, replace
		
// Fig A5. a		
gen b0_rawR = b0_EuroR_real 
gen h0_rawR = h0_EuroR_real 
gen l0_rawR = l0_EuroR_real 
gen h1_rawR = h1_EuroR_real 
gen l1_rawR = l1_EuroR_real

// baseline estimate on r-star, as well as estimation on trend for stock market		
					
foreach y of local response_trend {
	eststo clear
		forv h=0/40 {
		
						cap drop  d`y'`h'
						gen d`y'`h' = f`h'.`y'trend- l.`y'trend
						reg d`y'`h' pandemic100k l(0/10).pandemic100k  l(2/11).`y'trend 
												
						eststo
						replace b0_`y'  = _b[pandemic100k]                      if Horizon==`h'
						replace se0_`y' = _se[pandemic100k]                     if Horizon==`h'
						replace h0_`y'  = _b[pandemic100k]+2*_se[pandemic100k]  if Horizon==`h'
						replace l0_`y'  = _b[pandemic100k]-2*_se[pandemic100k]  if Horizon==`h'
						replace h1_`y'  = _b[pandemic100k]+1*_se[pandemic100k]  if Horizon==`h'
						replace l1_`y'  = _b[pandemic100k]-1*_se[pandemic100k]  if Horizon==`h'
						
						
					


}
}

// Figure A5 a
cap drop Zero
		gen Zero=0
		twoway ///
		(  rarea h0_EuroR_real l0_EuroR_real Horizon if Horizon~=. , fc(blue)  fi(10) lcolor(white)  lpattern(none)  )  ///
		(  rarea h1_EuroR_real l1_EuroR_real Horizon if Horizon~=. , fc(blue)  fi(20) lcolor(white) lw(none)  lpattern(none)  )  ///
		(  line  Zero    Horizon if Horizon~=. , lc(gray)  lw(med) lp(dash)                     )  ///		
		(  line  b0_EuroR_real     Horizon if Horizon~=. , lcolor(blue) lpattern(solid) lwidth(thick)) ///
		(  line  b0_rawR     Horizon if Horizon~=. , lc(green)  lw(thick) lp(dash)  ///
				xlab(0(10)40, labsize(large)) ylab(, angle(0) labsize(large)  nogrid) xtitle("Years since pandemic event ended",size(large)) ///
				ytitle("Percent",size(large)) ///
				legend(row(2) order(5 4) region(ls(none)) size(medsmall)  ///
			label(5 "raw real interest rate") label(4 "{it:r}*") ring(0) position(11) ))  ///
		, title()    ///
		xsize(5) ysize(4) scale(1.1) graphregion(color(white)) plotregion(color(white))  scheme(s1color) ///
		name(`y',replace) /// nodraw
		
		graph save   Appendix_Results/gphs/FigureA5a.gph, replace
		graph export Appendix_Results/FigureA5a.pdf, replace
		


